package com.polaris.live.widget

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.databinding.LayoutLiveEntryBinding
import com.polaris.live.resp.bean.LiveEffectBean
import com.polaris.live.utils.file.FileUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import org.libpag.PAGFile
import org.libpag.PAGScaleMode
import java.io.File

/**
 * 直播间进场飘屏
 * @author Created by 天晴 on 2024/1/29 14:30
 * @since 1.0.0
 **/
@SuppressLint("ResourceType")
class LiveEntryScreenItemView(
    context: Context,
    attrs: AttributeSet? = null,
) : ConstraintLayout(context, attrs) {

    private val mBinding = LayoutLiveEntryBinding.inflate(LayoutInflater.from(context), this, true)

    private lateinit var mData: LiveEffectBean
    private var mCombinedAnimatorSet: AnimatorSet? = null

    //结束回调
    private var mOnEntryScreenAnimationEnd: ((userId: Long) -> Unit)? = null

    init {
        visibility = INVISIBLE
        if (AppCodeUtils.isAR()) {
            //目前测试pag会反转过来的
            mBinding.pagView.scaleX = -1f
            mBinding.bgLl.setBackgroundResource(com.polaris.live.R.drawable.r20_back_51000000_000000_rtl)
            mBinding.bgLlBottom.setBackgroundResource(com.polaris.live.R.drawable.r20_back_51000000_000000_rtl)
        }
    }

    fun setData(
        bean: LiveEffectBean,
    ) {
        this.mData = bean
        mBinding.apply {
            val lp = root.layoutParams as LayoutParams
            if (AppCodeUtils.isAR()) {
                lp.rightMargin = 320.autoDp
            } else {
                lp.leftMargin = 320.autoDp
            }
            lp.width = 320.autoDp
            lp.height = 70.autoDp
            root.layoutParams = lp

            if (bean.isPag) {
                val path = FileUtils.getFilePath(FileUtils.getPagFile(), bean.data.url)
                if (File(path).exists()) {
                    val pagFile = PAGFile.Load(path)
                    mBinding.pagView.apply {
                        setScaleMode(PAGScaleMode.Zoom)
                        composition = pagFile
                        setRepeatCount(-1)
                        play()
                    }
                }
            } else {
                Glide.with(context)
                    .load(ImageUtils.imgPath(bean.data.url))
                    .centerCrop()
                    .into(image)
            }
            val headLayout = mBinding.avFv.layoutParams as LinearLayout.LayoutParams
            if (bean.avatarFrame?.isNotEmpty() == true) {
                headLayout.marginStart = 7.autoDp
            } else {
                headLayout.marginStart = 15.autoDp
            }
            mBinding.avFv.layoutParams = headLayout

            llLabel.setLabel(bean.label)

            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(bean.avatarFrame))
            avatar.setImageUrl(bean.avatar)
            lvLevel.setData(bean.sex, bean.richs, bean.charms)
            userName.text = bean.nickname
        }

    }

    //礼物动画分发展示
    fun startAnimation() {
        post {
            visibility = VISIBLE
            val animatorCenterX =
                ObjectAnimator.ofFloat(this, "translationX", 320.autoDp.toFloat(), 0f)
            val animatorAlpha = ObjectAnimator.ofFloat(this, "alpha", 0.2f, 1f)
            animatorCenterX.duration = 500
            animatorAlpha.duration = 500
            val animatorSet = AnimatorSet()
            animatorSet.playTogether(animatorCenterX, animatorAlpha)

            val animatorCenterX2 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, 0f)

            animatorCenterX2.duration = 3000

            val animatorCenterX3 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, -320.autoDp.toFloat())
            animatorCenterX3.duration = 500

            // 将两组动画结合起来
            mCombinedAnimatorSet = AnimatorSet()
            mCombinedAnimatorSet?.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    visibility = GONE
                    mOnEntryScreenAnimationEnd?.invoke(mData.userId)
                    mCombinedAnimatorSet = null
                }
            })
            mCombinedAnimatorSet?.playSequentially(animatorSet, animatorCenterX2, animatorCenterX3)
            mCombinedAnimatorSet?.start()
        }

    }

    //礼物动画分发展示RtL
    fun startRtLAnimation() {
        post {
            visibility = VISIBLE
            val animatorCenterX =
                ObjectAnimator.ofFloat(this, "translationX", -320.autoDp.toFloat(), 0f)
            val animatorAlpha = ObjectAnimator.ofFloat(this, "alpha", 0.2f, 1f)
            animatorCenterX.duration = 500
            animatorAlpha.duration = 500
            val animatorSet = AnimatorSet()
            animatorSet.playTogether(animatorCenterX, animatorAlpha)

            val animatorCenterX2 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, 0f)

            animatorCenterX2.duration = 3000

            val animatorCenterX3 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, 320.autoDp.toFloat())
            animatorCenterX3.duration = 500

            // 将两组动画结合起来
            mCombinedAnimatorSet = AnimatorSet()
            mCombinedAnimatorSet?.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    visibility = GONE
                    mOnEntryScreenAnimationEnd?.invoke(mData.userId)
                    mCombinedAnimatorSet = null
                }
            })
            mCombinedAnimatorSet?.playSequentially(animatorSet, animatorCenterX2, animatorCenterX3)
            mCombinedAnimatorSet?.start()
        }

    }

    fun cancelAnimation() {
        mCombinedAnimatorSet?.cancel()
        mCombinedAnimatorSet = null
    }

    fun setOnEntryScreenAnimationEnd(click: (userId: Long) -> Unit) {
        mOnEntryScreenAnimationEnd = click
    }

}